home *** CD-ROM | disk | FTP | other *** search
/ Aminet 34 / Aminet 34 (2000)(Schatztruhe)[!][Dec 1999].iso / Aminet / util / gnu / unixcmds.lha / unixcmds / src / basename.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-06  |  2.6 KB  |  92 lines

  1. /* basename - print last part of a path      Authors: B. Garfolo & P. Nelson */
  2.  
  3. /* Basename - print the last part of a path.
  4.  *
  5.  *    For MINIX  --  Conforms to POSIX - P1003.2/D10
  6.  *      Exception -- it ignores the LC environment variables.
  7.  *
  8.  *    Original MINIX author:  Blaine Garfolo
  9.  *    POSIX rewrite author:   Philip A. Nelson
  10.  *
  11.  *    POSIX version - October 20, 1990
  12.  *      Feb 14, 1991: changed rindex to strrchr. (PAN)
  13.  *
  14.  */
  15.  
  16. #include <string.h>
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19.  
  20. #define EOS '\0'
  21.  
  22. int main (int argc, char **argv);
  23.  
  24. int main(argc, argv)
  25. /* [<][>][^][v][top][bottom][index][help] */
  26. int argc;
  27. char *argv[];
  28. {
  29.   char *result_string;          /* The pointer into argv[1]. */
  30.   char *temp;                   /* Used to move around in argv[1]. */
  31.   int suffix_len;               /* Length of the suffix. */
  32.   int suffix_start;             /* Where the suffix should start. */
  33.   int argbeg=1,amiga_mode=0;
  34.  
  35.   /* Check for the correct number of arguments. */
  36.   if ((argc < 2) || (argc > 4)) {
  37.         fprintf(stderr, "Usage: basename [--amiga] string [suffix] \n");
  38.         exit(1);
  39.   }
  40.  
  41.   if (!strcmp(argv[1],"--amiga"))
  42.   {
  43.     argbeg++;
  44.     amiga_mode=1;
  45.   }
  46.  
  47.   /* Check for all /'s */
  48.   for (temp = argv[argbeg]; (*temp == '/') || ((amiga_mode)&&(*temp==':')); temp++)    /* Move to next char. */
  49.         ;
  50.   if (*temp == EOS) {
  51.         printf("/\n");
  52.         exit(0);
  53.   }
  54.  
  55.   /* Build the basename. */
  56.   result_string = argv[argbeg];
  57.  
  58.   /* Find the last /'s or :'s if amiga_mode is on */
  59.   temp = strrchr(result_string, '/');
  60.   if ((temp == NULL)&&(amiga_mode)) {
  61.   temp = strrchr(result_string, ':');
  62.   }
  63.  
  64.   if (temp != NULL) {
  65.         /* Remove trailing /'s. */
  66.         while ((*(temp + 1) == EOS) && ((*temp == '/') 
  67.             || ((amiga_mode==0)||(*temp == ':'))) ) *temp-- = EOS;
  68.  
  69.         /* Set result_string to last part of path. */
  70.         if ((*temp != '/') && ((amiga_mode==0)||(*temp != ':')))
  71.     { temp = strrchr(result_string, '/'); 
  72.       if ((temp==NULL)&&(amiga_mode)) temp = strrchr(result_string, ':'); }
  73.  
  74.       if (temp != NULL && ((*temp == '/') || ((amiga_mode==0)||(*temp == ':')) ))
  75.         { result_string = temp + 1; }
  76.   }
  77.  
  78.   /* Remove the suffix, if any. */
  79.   if (argc-amiga_mode > 2) {
  80.         suffix_len = strlen(argv[argbeg+1]);
  81.         suffix_start = strlen(result_string) - suffix_len;
  82.         if (suffix_start > 0)
  83.                 if (strcmp(result_string + suffix_start, argv[argbeg+1]) == EOS)
  84.                         *(result_string + suffix_start) = EOS;
  85.   }
  86.  
  87.   /* Print the resultant string. */
  88.   printf("%s\n", result_string);
  89.   return(0);
  90. }
  91. /* [<][>][^][v][top][bottom][index][help] */
  92.